package com.mz.ozy.adapter;

import android.app.Activity;
import android.text.TextUtils;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.mz.ozy.R;
import com.mz.ozy.activity.c2c.PhotoScanActivity;
import com.mz.ozy.bean.DynamicsBean;
import com.mz.ozy.bean.EventBean;
import com.mz.ozy.bean.ReplyBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.utils.CalendarUtils;
import com.mz.ozy.utils.ScreenUtil;
import com.mz.ozy.utils.Utils;
import com.mz.ozy.utils.glideUtils.GlideUtils;
import com.mz.ozy.widget.FriendsCircleImageLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.ParseException;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2019/3/27.
 */

public class TopicItemAdapter extends BaseQuickAdapter<DynamicsBean, BaseViewHolder> {
    private final Activity mActivity;
    private final long curTime;
    private int mCollPosition = -1;
    private onCommentListener mOnCommentListener;

    public void setCollPosition(int position) {
        mCollPosition = position;
    }

    public TopicItemAdapter(Activity activity, List<DynamicsBean> data) {
        super(R.layout.item_topic_detail, data);
        mActivity = activity;
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        Date curDate = cal.getTime();
        curTime = curDate.getTime();
        EventBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(EventBean event) {
        if (event.getCode() == Constants.EventCode.LIKE_DYNAMICS) {
            setCellLikeState();
        }
    }

    @Override
    protected void convert(BaseViewHolder helper, DynamicsBean item) {
        helper.setText(R.id.iv_friend_dynamics_name, item.getNickname());
        Date itemDate = null;
        try {
            itemDate = CalendarUtils.string2Date(item.getCreated());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (itemDate != null) {
            helper.setText(R.id.iv_friend_dynamics_time, CalendarUtils.getFriendlytime(itemDate));
        } else {
            helper.setText(R.id.iv_friend_dynamics_time, "刚刚");
        }
        helper.setText(R.id.tv_friend_dynamics_content, Utils.getDecodeStr(item.getContent()));
//        helper.setText(R.id.tv_friend_dynamics_content, item.getContent());
        helper.setText(R.id.tv_friend_dynamics_address, item.getReAddress());
        if (!TextUtils.isEmpty(item.getTitle())) {
            helper.setVisible(R.id.tv_dynamic_topic, true);
            helper.setText(R.id.tv_dynamic_topic, "#" + item.getTitle() + "#");
        } else {
            helper.setGone(R.id.tv_dynamic_topic, false);
        }
        if (!TextUtils.isEmpty(Constants.uid)) {
            int uid = Integer.parseInt(Constants.uid);
            if (uid == item.getUid()) {
                helper.getView(R.id.tv_friend_dynamics_deletes).setVisibility(View.VISIBLE);
            } else {
                helper.getView(R.id.tv_friend_dynamics_deletes).setVisibility(View.GONE);
            }
        } else {
            helper.getView(R.id.tv_friend_dynamics_deletes).setVisibility(View.GONE);
        }

        if (0 == item.getIsCollection()) {
            helper.setImageResource(R.id.iv_friend_dynamics_like, R.mipmap.dynamic_no_like);
        } else {
            helper.setImageResource(R.id.iv_friend_dynamics_like, R.mipmap.dynamic_like);
        }

        helper.setText(R.id.tv_friend_dynamics_likenum, item.getZanNum() + "");
        helper.setText(R.id.tv_friend_dynamics_commentnum, item.getComNum() + "");

        FriendsCircleImageLayout circleImageLayout = helper.getView(R.id.fcil_friend_dynamics_imgs);
        if (!TextUtils.isEmpty(item.getImg())) {
//            Integer[] mImages1 = new Integer[]{R.mipmap.one, R.mipmap.three};
//            item.setImages(mImages1);
            String[] mImages = item.getImg().split(",");
            circleImageLayout.setImageUrls(Arrays.asList(mImages), new FriendsCircleImageLayout.onImageListener() {
                @Override
                public void onClick(int pos) {
                    PhotoScanActivity.startPhotoScan(mActivity, Arrays.asList(mImages), pos);
//                    Intent intent = new Intent(mContext, DynamicDetailActivity.class);
//                    intent.putExtra(DynamicDetailActivity.DYNAMICS_ID, item.getId());
//                    mContext.startActivity(intent);
                }
            });
            circleImageLayout.setVisibility(View.VISIBLE);
        } else {
            circleImageLayout.setVisibility(View.GONE);
        }

        if (TextUtils.isEmpty(item.getUseravatar())) {
            GlideUtils.loadImg(mContext, helper.getView(R.id.iv_friend_head), R.mipmap.default_user_head);
        } else {
            GlideUtils.loadBackImg(mContext, helper.getView(R.id.iv_friend_head), item.getUseravatar(), R.mipmap.default_user_head);
        }

        final int dynamicid = item.getId();
        RecyclerView mRvFriendComments = helper.getView(R.id.rv_friend_comments);
//        if (item.getComlist() != null && item.getComlist().size() > 0) {
//            if (mRvFriendComments.getAdapter() == null) {
        ReplyListAdapter commentReplyAdapter = new ReplyListAdapter(item.getComlist());
        mRvFriendComments.setLayoutManager(new LinearLayoutManager(mContext));
        mRvFriendComments.setAdapter(commentReplyAdapter);
        commentReplyAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                final ReplyBean replyBean = commentReplyAdapter.getItem(position);
                switch (view.getId()) {
                    case R.id.tv_reply_name: {
                        mOnCommentListener.onClickUser(replyBean.getUid() + "");
                        break;
                    }
                    case R.id.tv_host_name: {
                        mOnCommentListener.onClickUser(replyBean.getUuid());
                        break;
                    }
                    case R.id.tv_reply_label:
                    case R.id.tv_reply_content: {
                        mOnCommentListener.onCommentClick(dynamicid, replyBean);
                        break;
                    }
                }
            }
        });
        if (item.getComlist() != null && item.getComlist().size() > 0) {
            mRvFriendComments.setPadding(ScreenUtil.dip2px(mActivity, 10), ScreenUtil.dip2px(mActivity, 10), ScreenUtil.dip2px(mActivity, 10), 0);
        } else {
            mRvFriendComments.setPadding(0, 0, 0, 0);
        }
//            } else {
//                ReplyListAdapter adapter = (ReplyListAdapter) mRvFriendComments.getAdapter();
//                adapter.setNewData(item.getComlist());
//            }
//        } else {
//            mRvFriendComments.setVisibility(View.GONE);
//        }
        helper.addOnClickListener(R.id.ll_friend_dynamics_like);
        helper.addOnClickListener(R.id.ll_friend_dynamics_comment);
        helper.addOnClickListener(R.id.tv_friend_dynamics_deletes);
        helper.addOnClickListener(R.id.iv_friend_head);
        helper.addOnClickListener(R.id.item_dynamics_view);
    }

    public void setCellLikeState() {
        if (mCollPosition != -1) {
            DynamicsBean item = getItem(mCollPosition);
            if (0 == item.getIsCollection()) {
                item.setIsCollection(1);
                item.setZanNum(item.getZanNum() + 1);
            } else {
                item.setIsCollection(0);
                item.setZanNum(item.getZanNum() - 1);
            }
            notifyDataSetChanged();
        }
    }

    public void setOnCommentListener(onCommentListener listener) {
        mOnCommentListener = listener;
    }

    public interface onCommentListener {
        void onCommentClick(int dynamicid, ReplyBean replyBean);

        void onClickUser(String uid);
    }
}
